package main

import (
	"changeme/Z"
	"changeme/controller"
	"changeme/module"
	"context"
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"strconv"
)

// App struct
type App struct {
	ctx context.Context
}

// NewApp creates a new App application struct
func NewApp() *App {
	return &App{}
}

// startup is called at application startup
func (a *App) startup(ctx context.Context) {
	// Perform your setup here
	a.ctx = ctx
}

// domReady is called after the front-end dom has been loaded
func (a App) domReady(ctx context.Context) {
	// Add your action here
}

// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
	// Perform your teardown here
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
	return fmt.Sprintf("Hello %s!", name)
}

// GetInitConfig 检测是否存在配置信息并请求文章列表
func (a App) GetInitConfig() Z.H {
	var initYamlConModel module.InitYamlConModel
	conFilePath := "./config/init.yaml"
	file, _ := ioutil.ReadFile(conFilePath)
	err := yaml.Unmarshal(file, &initYamlConModel)
	if err != nil {
		fmt.Println("解析配置信息错误：", err)
	}

	if initYamlConModel.Init.WebUrl == "" || initYamlConModel.Init.Username == "" || initYamlConModel.Init.Password == "" {
		return Z.H{
			"code": 400,
			"msg":  initYamlConModel.Init.WebUrl,
		}
	} else {
		//登录获取token
		res_t := controller.LoginGetToken(initYamlConModel.Init.Username, initYamlConModel.Init.Password, initYamlConModel.Init.WebUrl)
		if res_t == false {
			return Z.H{
				"code": 400,
				"msg":  "登录失败",
			}
		}
		//请求文章列表
		res := controller.GetBlogPosts(initYamlConModel.Init.WebUrl)
		return res
	}
}

func (a App) PutBlog(key int, title string, content string) Z.H {
	res := controller.PutBlog(strconv.Itoa(key), title, content)
	return res
}

func (a App) Login(url string, username string, password string) Z.H {
	res := controller.LoginGetToken(username, password, url)
	if res {
		controller.Login(username, password, url)
		return Z.H{
			"code": 200,
			"msg":  "登录成功",
		}
	} else {
		return Z.H{
			"code": 400,
			"msg":  "登录失败",
		}
	}
}

func (a App) ReleaseBlog(title string, content string) map[string]interface{} {
	res := controller.ReleaseBlog(title, content)
	return res
}

func (a App) GetBlogPosts() Z.H {
	var initYamlConModel module.InitYamlConModel
	conFilePath := "./config/init.yaml"
	file, _ := ioutil.ReadFile(conFilePath)
	err := yaml.Unmarshal(file, &initYamlConModel)
	if err != nil {
		fmt.Println("解析配置信息错误：", err)
	}
	res := controller.GetBlogPosts(initYamlConModel.Init.WebUrl)
	return res
}
func (a App) PostPages(page int) Z.H {
	var initYamlConModel module.InitYamlConModel
	conFilePath := "./config/init.yaml"
	file, _ := ioutil.ReadFile(conFilePath)
	err := yaml.Unmarshal(file, &initYamlConModel)
	if err != nil {
		fmt.Println("解析配置信息错误：", err)
	}
	res := controller.PostPages(initYamlConModel.Init.WebUrl, strconv.Itoa(page))
	return res
}

func (a App) DeleteArticle(id int) map[string]interface{} {
	res := controller.DeleteArticle(strconv.Itoa(id))
	return res
}
